package com.pj.lib.auraClient;

import java.util.ArrayList;

import com.pj.lib.auraClient.API.AuraCaster;
import com.pj.lib.auraClient.API.AuraSensitiveUser;
import com.pj.lib.auraClient.API.AuraUser;
import com.pj.lib.auraClient.interfaces.OnServiceConnection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/*
 * Controller of aura interface
 */
public class Aura {
	public static final String TAG = "Aura";
	public static boolean DEBUG = true; 
	
	private static Aura asc;
	private static Boolean inited = false;
	private static AuraConfiguration conf;
	
	private AuraServiceConnector mServiceConnector; 
	private AuraService mBoundedAuraService;
	private AuraCaster mAuraCaster;
	private AuraSensitiveUser mAuraSensitiveUser;
	private AuraUser mAuraUser;
	
	private ArrayList<OnServiceConnection> mOnServiceConnectionListeners = new ArrayList<OnServiceConnection>();
	
	private Aura() {
		//set debugable
		Aura.DEBUG = conf.isDebug();
		
		//initialize components
		Intent intent = new Intent(conf.getContext(), AuraService.class);
		intent.putExtra("serverAddress", conf.getServerAddress());
		intent.putExtra("serverPort", conf.getServerPort());
		Log.i(TAG, "starting service");
		conf.getContext().startService(intent);
		
		/*while(AuraService.STARTED == false) {
			//Log.i(TAG, "waiting for service to start");
		}*/
		mServiceConnector = new AuraServiceConnector(this, conf.getContext());
		
		mAuraCaster = new AuraCaster(this);
		mAuraSensitiveUser = new AuraSensitiveUser(this);
		mAuraUser = new AuraUser(this);
		
	};
	
	public static Aura getInstance() {
		if(inited) {
			if(asc == null) asc = new Aura();
			
			return asc;
		} else {
			Log.e(TAG, "Aura not initialized. First initialize Aura by Aura.init() method");
			return null;
		}
	}
	
	public static void init(AuraConfiguration conf) {
		if(conf.getContext() != null &&
				conf.getServerAddress() != null &&
				conf.getServerPort() > 0) {
			Aura.conf = conf;
			inited = true;
		} else {
			Log.e(TAG, "Wrong configuration object. Please change your AuraConfiguration.");
		}
	}
	
	public AuraUser getUserAPI() {
		return mAuraUser;
	}
	public AuraSensitiveUser getAuraSensitiveUserAPI() {
		return mAuraSensitiveUser;
	}
	public AuraCaster getAuraCasterAPI() {
		return mAuraCaster;
	}
	
	public Context getContext() {
		return conf.getContext();
	}
	
	
	public AuraService getService() {
		if(mBoundedAuraService == null) {
			mBoundedAuraService = mServiceConnector.getService();
		}
		return mBoundedAuraService;
	}
	public Boolean isServiceBounded() {
		return mServiceConnector.isBounded();
	}
	
	public void addOnServiceConnectionListener(OnServiceConnection listener) {
		mOnServiceConnectionListeners.add(listener);
	}
	
	public void fireServiceConnection(Boolean isConnected) {
		for(OnServiceConnection listener : mOnServiceConnectionListeners) {
			listener.serviceConnection(isConnected);
		}
	}
}
